package com.itheima.reggie.controller;


import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

//分类列表
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private HttpSession httpSession;

    //查询全部分类
    @GetMapping("/category/findAll")
    public ResultInfo findAll(){
        List<Category> categoryList = categoryService.findAll();
        return categoryList != null ? ResultInfo.success(categoryList) : ResultInfo.error("查询失败");
    }

    //新增菜品和套餐分类
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category){

        category.setId(IdUtil.getSnowflake(1,1).nextId());
//        category.setCreateTime(new Date());
//        Employee employee = (Employee) httpSession.getAttribute("SESSION_EMPLOYEE");
//        category.setCreateUser(employee.getId());
//        category.setUpdateTime(new Date());
//        category.setUpdateUser(employee.getId());

        Integer addNum = categoryService.save(category);

        return addNum > 0 ? ResultInfo.success(category) : ResultInfo.error("添加失败");
    }

    //删除分类
    @DeleteMapping("/category")
    public ResultInfo delete(long id){
        Integer deleteNum = categoryService.delete(id);
        return deleteNum > 0 ? ResultInfo.success(null) : ResultInfo.error("删除失败");
    }

    //菜品添加时选择菜品分类
    @GetMapping("/category/list")
    public ResultInfo findCategory(Integer type){
        List<Category> categoryList =  categoryService.findCategory(type);
        System.out.println(type);
        return ResultInfo.success(categoryList);
    }


}